'use strict';
const semver = require('semver')
const colors = require('colors')
const log = require("@hl-imooc-cli-dev/log");
const LOWEST_NODE_VERSION = '12.0.0'
class Command {
    // TODO
    constructor(argv) {
        // log.verbose('Ccmmand constructo', argv)
        this._argv = argv;
        if(!this._argv){
            throw new Error('参数不能为空')
        }
        if(!Array.isArray(argv)){
            throw new Error('参数不能为数组')
        }
        if(argv.length < 1) {
            throw new Error('参数列表不能为空')
        }
        let runner = new Promise((resolve, reject) => {
            let chain = Promise.resolve();
            chain = chain.then(() => {
                this.checkNodeVersion();
            })
            chain = chain.then(() => {
                this.initArgs()
            })
            chain  = chain.then(() => {
                this.init()
            })
            chain  = chain.then(() => {
                this.exec()
            })
            chain.catch(err=> {
                log.error(err.message)
            })
        })
    }
    initArgs(){
        // console.log('_argv>>>>>',Object.keys(this._argv).forEach((key) => console.log('key====',this._argv[key])))
        this._cmd = this._argv[this._argv.length -1];
        this._argv =  this._argv.slice(0,  this._argv.length -1)

    }
    checkNodeVersion() {
        // 第一步 获取当前版本号
        const currentVersion = process.version;
        const lowestNodeVersion = LOWEST_NODE_VERSION;
        // 第二步 比对最低版本号
        if (!semver.gte(currentVersion, lowestNodeVersion)) {
          throw new Error(
            colors.red(`hl-imooc-cli 需要安装${lowestNodeVersion}以上版本的 Node.js`)
          );
        }
      }
    init() {
        throw new Error('init 必须实现');
    }

    exec(){
        throw new Error('exec 必须实现');
    }
}

module.exports = Command;
